package com.schoolcloud.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.schoolcloud.system.entity.SysPermission;
import com.schoolcloud.system.entity.SysRole;
import com.schoolcloud.system.entity.SysRolePermission;
import com.schoolcloud.system.mapper.SysRolePermissionMapper;
import com.schoolcloud.system.service.SysPermissionService;
import com.schoolcloud.system.service.SysRolePermissionService;
import com.schoolcloud.system.service.SysRoleService;
import com.schoolcloud.system.dto.SysRoleCreateDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【sys_role_permission】的数据库操作Service实现
 * @createDate 2024-10-19 23:09:28
 */
@Service
@RequiredArgsConstructor
public class SysRolePermissionServiceImpl implements SysRolePermissionService {
    private final SysPermissionService sysPermissionService;
    private final SysRoleService sysRoleService;
    private final SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public SysRoleCreateDTO getRoleInfoByRoleId(String roleId) {
        SysRole roleById = sysRoleService.getRoleById(roleId);
        List<Integer> permissionIds = sysPermissionService.getPermissionsByRoleId(roleId).stream().map(SysPermission::getPermissionId).collect(Collectors.toList());
        SysRoleCreateDTO sysRoleInfo = new SysRoleCreateDTO();
        BeanUtils.copyProperties(roleById, sysRoleInfo);
        sysRoleInfo.setPermissionIds(permissionIds);
        return sysRoleInfo;
    }

    @Override
    public void createSysRole(SysRoleCreateDTO createDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(createDTO, sysRole);
        sysRoleService.createSysRole(sysRole);
        List<Integer> permissionIds = createDTO.getPermissionIds();
        if (CollUtil.isNotEmpty(permissionIds)){
            for (Integer permissionId : permissionIds) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setPermissionId(permissionId);
                sysRolePermission.setRoleId(createDTO.getRoleId());
                sysRolePermissionMapper.addRolePermission(sysRolePermission);
            }
        }
    }

    @Override
    @Transactional
    public void modifySysRole(SysRoleCreateDTO createDTO) {
        sysRoleService.modifySysRole(createDTO);
        List<Integer> permissionIds = createDTO.getPermissionIds();
        if (CollUtil.isNotEmpty(permissionIds)){
//            先删除该角色下的所有权限
            sysRolePermissionMapper.delRolePermissionByRoleId(createDTO.getRoleId());
//            然后再执行新增
            for (Integer permissionId : permissionIds) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setPermissionId(permissionId);
                sysRolePermission.setRoleId(createDTO.getRoleId());
                sysRolePermissionMapper.addRolePermission(sysRolePermission);
            }
        }
    }
}
